package com.ruoyi.cms.article.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cms.article.domain.CmsArticle;
import com.ruoyi.cms.article.domain.CmsArticleTag;
import com.ruoyi.cms.article.domain.CmsArticleType;
import com.ruoyi.cms.article.domain.vo.CmsArticleSearchVo;
import com.ruoyi.cms.article.domain.vo.SiteCmsArticleSelectVo;
import com.ruoyi.cms.article.mapper.CmsArticleMapper;
import com.ruoyi.cms.article.mapper.CmsArticleTypeMapper;
import com.ruoyi.cms.article.service.ICmsArticleService;
import com.ruoyi.cms.tag.domain.CmsTag;
import com.ruoyi.cms.type.domain.CmsType;
import com.ruoyi.cms.type.mapper.CmsTypeMapper;
import com.ruoyi.common.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service
public class CmsArticleService extends ServiceImpl<CmsArticleMapper, CmsArticle> implements ICmsArticleService {

    @Resource
    private CmsArticleMapper cmsArticleMapper;

    @Resource
    private CmsArticleTypeMapper cmsArticleTypeMapper;

    @Resource
    private CmsTypeMapper cmsTypeMapper;

    /**
     * 查询文章管理
     *
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public CmsArticle selectCmsArticleById(Long id) {
        CmsArticle article = cmsArticleMapper.selectCmsArticleById(id);
        //查询标签列表
        List<CmsType> types = new ArrayList<>();
        //查询分类列表
        List<CmsArticleType> articleTypeList = cmsArticleTypeMapper.selectArticleTypeList(id);
        Long[] typeIds = new Long[articleTypeList.size()];
        for (int i = 0; i < articleTypeList.size(); i++) {
            CmsArticleType cmsArticleType = articleTypeList.get(i);
            Long typeId = cmsArticleType.getTypeId();
            typeIds[i] = typeId;
            CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(typeId);
            types.add(cmsType);
        }
        article.setTypeIds(typeIds);
        article.setTypes(types);
        return article;
    }

    /**
     * 查询文章管理列表
     *
     * @param cmsArticle 文章管理
     * @return 文章管理
     */
    @Override
    public List<CmsArticle> selectCmsArticleList(CmsArticle cmsArticle) {
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleList(cmsArticle);
        return articleListAddType(cmsArticleList);
    }


    /**
     * 查询文章管理列表
     *
     * @param vo
     * @return
     */
    public List<CmsArticle> selectCmsArticleVoList(CmsArticleSearchVo vo) {
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleVoList(vo);
        return articleListAddType(cmsArticleList);
    }


    /**
     * 查询文章管理列表
     *
     * @param vo
     * @return
     */
    @Override
    public List<CmsArticle> selectCmsArticleVoListForSite(SiteCmsArticleSelectVo vo) {
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleVoListForSite(vo);
        return articleListAddType(cmsArticleList);
    }


    /**
     * 查询文章管理列表
     *
     * @param vo
     * @return
     */
//    @Override
//    public List<CmsArticle> selectCmsArticleTopVoList(SiteCmsArticleSelectVo vo) {
//        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleTopVoList(vo);
//        return articleListAddType(cmsArticleList);
//    }


    /**
     * 查询推荐文章列表
     */
    @Override
    public List<CmsArticle> selectCmsArticleListRecommend(CmsArticle cmsArticle) {
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleListRecommend(cmsArticle);
        return articleListAddType(cmsArticleList);
    }

    /**
     * 按分类查询文章列表
     */
    @Override
    public List<CmsArticle> selectCmsArticleListByTypeId(Long id) {
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleListByTypeId(id);
        return articleListAddType(cmsArticleList);
    }

    /**
     * 按标签查询文章列表
     */
    @Override
    public List<CmsArticle> selectCmsArticleListByTagId(Long id) {
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectCmsArticleListByTagId(id);
        return articleListAddType(cmsArticleList);
    }

    /**
     * 新增文章管理
     *
     * @param cmsArticle 文章管理
     * @return 结果
     */
    @Override
    public Long insertCmsArticle(CmsArticle cmsArticle) {
        if (ObjectUtil.isEmpty(cmsArticle.getCreateTime())) {
            cmsArticle.setCreateTime(DateUtils.getNowDate());
        }
        cmsArticleMapper.insertCmsArticle(cmsArticle);
        Long articleId = cmsArticle.getId();
        //新增文章分类
        Long[] typeIds = cmsArticle.getTypeIds();
        if (typeIds != null && typeIds.length > 0) {
            List<CmsArticleType> articleTypeList = new ArrayList<>();
            for (Long typeId : typeIds) {
                CmsArticleType cmsArticleType = new CmsArticleType();
                cmsArticleType.setArticleId(articleId);
                cmsArticleType.setTypeId(typeId);
                articleTypeList.add(cmsArticleType);
            }
            cmsArticleTypeMapper.batchArticleType(articleTypeList);
        }
        return articleId;
    }

    /**
     * 修改文章管理
     *
     * @param cmsArticle 文章管理
     * @return 结果
     */
    @Override
    public int updateCmsArticle(CmsArticle cmsArticle) {
        cmsArticle.setUpdateTime(DateUtils.getNowDate());
        Long articleId = cmsArticle.getId();
        //清空文章分类关联
        cmsArticleTypeMapper.deleteArticleTypeByArticleId(articleId);
        //新增文章分类
        Long[] typeIds = cmsArticle.getTypeIds();
        if (typeIds != null && typeIds.length > 0) {
            List<CmsArticleType> articleTypeList = new ArrayList<>();
            for (Long typeId : typeIds) {
                CmsArticleType cmsArticleType = new CmsArticleType();
                cmsArticleType.setArticleId(articleId);
                cmsArticleType.setTypeId(typeId);
                articleTypeList.add(cmsArticleType);
            }
            cmsArticleTypeMapper.batchArticleType(articleTypeList);
        }
        return cmsArticleMapper.updateCmsArticle(cmsArticle);
    }

    /**
     * 批量删除文章管理
     *
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleByIds(Long[] ids) {
        for (Long id : ids) {
            //清空文章分类关联
            cmsArticleTypeMapper.deleteArticleTypeByArticleId(id);
        }
        return cmsArticleMapper.deleteCmsArticleByIds(ids);
    }

    /**
     * 删除文章管理信息
     *
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleById(Long id) {
        //清空文章分类关联
        cmsArticleTypeMapper.deleteArticleTypeByArticleId(id);
        return cmsArticleMapper.deleteCmsArticleById(id);
    }


    /**
     * 查询当前分类id下文章的数量
     *
     * @param typeId
     * @return
     */
    @Override
    public Long selectCmsArticleCountByTypeId(Long typeId) {
        LambdaQueryWrapper<CmsArticleType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CmsArticleType::getTypeId, typeId);
        return cmsArticleTypeMapper.selectCount(lambdaQueryWrapper);
    }


    /**
     * 根据cmsArticleList添加对应的typeIds
     *
     * @param cmsArticleList
     * @return
     */
    private List<CmsArticle> articleListAddType(List<CmsArticle> cmsArticleList) {
        if (cmsArticleList == null || cmsArticleList.size() < 0) {
            return cmsArticleList;
        }
        for (CmsArticle article : cmsArticleList) {
            Long articleId = article.getId();
            //查询分类列表
            List<CmsArticleType> articleTypeList = cmsArticleTypeMapper.selectArticleTypeList(articleId);
            List<CmsType> cmsTypeList = new ArrayList<>();
            List<Long> typeIds = new ArrayList<>();
            articleTypeList.forEach((CmsArticleType cmsArticleType) -> {
                Long typeId = cmsArticleType.getTypeId();
                CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(typeId);
                if (ObjectUtil.isNotEmpty(cmsType)) {
                    cmsTypeList.add(cmsType);
                    typeIds.add(typeId);
                }
            });
            article.setTypes(cmsTypeList);
            article.setTypeIds(typeIds.stream().toArray(Long[]::new));
        }
        return cmsArticleList;
    }


    /**
     * 查询文章列表（网站前台）
     *
     * @param typeId 分类的id
     * @return
     */
//    public List<CmsArticle> selectCmsArticleListForSite(@Param("typeId")Long typeId){
//        cmsArticleTypeMapper.selectCmsArticleListForSite
//    }

    /**
     * 查询文章列表（网站前台）
     *
     * @param typeId 分类的id
     * @param count  返回的条数
     * @return
     */
    @Override
    public List<CmsArticle> selectCmsArticleTopVoListForSite(Long typeId, Long count) {
        return cmsArticleMapper.selectCmsArticleTopVoListForSite(typeId, count);
    }


    /**
     * 查询文章详细（网站前台）
     * 该方法会将文章的类别限定在typeId
     *
     * @param typeId
     * @param articleId
     * @return
     */
    @Override
    public CmsArticle selectCmsArticleDetailForSite(Long typeId, Long articleId) {
        LambdaQueryWrapper<CmsArticle> cmsArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cmsArticleLambdaQueryWrapper.eq(CmsArticle::getId, articleId);
        CmsArticle cmsArticle = cmsArticleMapper.selectOne(cmsArticleLambdaQueryWrapper);
        if (ObjectUtil.isNotEmpty(cmsArticle)) {
            CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(typeId);
            cmsArticle.setType(cmsType.getTypeName());
            cmsArticle.setTypes(new ArrayList<>(Collections.singletonList(cmsType)));
            cmsArticle.setTypeIds(Stream.of(cmsType).map(CmsType::getTypeId).toArray(Long[]::new));
            return cmsArticle;
        } else {
            return null;
        }
    }


    /**
     * 查询文章详细（网站前台）
     *
     * @param articleId
     * @return
     */
//    public CmsArticle selectCmsArticleDetailForSite(Long articleId) {
//        LambdaQueryWrapper<CmsArticleType> cmsArticleTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //cmsArticleTypeLambdaQueryWrapper.eq(CmsArticleType::getTypeId, typeId);
//        cmsArticleTypeLambdaQueryWrapper.eq(CmsArticleType::getArticleId, articleId);
//        List<CmsArticleType> cmsArticleTypeList = cmsArticleTypeMapper.selectList(cmsArticleTypeLambdaQueryWrapper);
//        if (ObjectUtil.isNotEmpty(cmsArticleTypeList) && !cmsArticleTypeList.isEmpty()) {
//            CmsArticleType cmsArticleType = cmsArticleTypeList.get(0);
//            CmsArticle cmsArticle = cmsArticleMapper.selectCmsArticleById(cmsArticleType.getArticleId());
//            CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(articleId);
//            cmsArticle.setType(cmsType.getTypeName());
//            cmsArticle.setTypes(new ArrayList<>(Collections.singletonList(cmsType)));
//            cmsArticle.setTypeIds(Stream.of(cmsType).map(CmsType::getTypeId).toArray(Long[]::new));
//            return cmsArticle;
//        } else {
//            return null;
//        }
//    }
}
